﻿using MailBox.App_Code;
using MailBox.App_Code.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MailBox
{
    public partial class Profile : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static AccountDTO LoadData()
        {
            UserLoginDTO user = (UserLoginDTO)HttpContext.Current.Session["UserLogin"];
            DataUtils data = new DataUtils();
            AccountDTO dto = user.Account;
            dto.TenCongTy = user.Client.CompanyName;
            if (dto != null)
                if (string.IsNullOrWhiteSpace(dto.Avatar))
                    dto.Avatar = "images/1386966026professional-man.png";
            return dto;
        }
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static AccountDTO Save(string avatar, string name, string phone, string birthday, string address)
        {
            UserLoginDTO user = (UserLoginDTO)HttpContext.Current.Session["UserLogin"];
            DataUtils data = new DataUtils();
            AccountDTO dto = user.Account;// data.GetAccountByID(user.Account.AccountID);
            if (dto != null)
            {
                dto.Avatar = avatar;
                dto.AccountName = name;
                dto.Phone = phone;
                DateTime? bd = Common.convertStringToDate(birthday);
                dto.BirthDay = bd;
                dto.Address = address;
                data.UpdateAccountInfo(dto);
                user.Account = dto;
                HttpContext.Current.Session["UserLogin"] = user;
            }
            return dto;
        }
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public static string ChangePass(string currentpass, string newpass)
        {
            try
            {
                UserLoginDTO user = (UserLoginDTO)HttpContext.Current.Session["UserLogin"];
                string inputOldPass = Common.GetMd5Hash(currentpass);
                string message = user.Account.Password == inputOldPass ? "" : "Mật khẩu hiện tại không đúng!";
                if (message == "")
                {
                    DataUtils data = new DataUtils();
                    newpass = Common.GetMd5Hash(newpass);
                    data.AccountChangePass(user.Account.Email, newpass);
                    user.Account.Password = newpass;
                    HttpContext.Current.Session["UserLogin"] = user;

                    return "Đã thay đổi mật khẩu thành công !";
                }
                else
                {
                    return message;
                }
            }
            catch (Exception ex)
            {
                Common.logs(null,HttpContext.Current.Request.RawUrl + "- ChangePass", ex, Common.logType.error);
                return ex.Message;
            }
        }
    }
}